package com.itheima.health.util;

import org.mindrot.jbcrypt.BCrypt;
import org.springframework.util.DigestUtils;

/**
 * @author ：wf
 * @date ：Created in 2022年06月08日
 * @description ：判断用户密码用的那种加密方式   通过查询数据库
 * @version: 1.0
 */
public class PasswordUtils {
    public static boolean checkPassword(String password,String dbPassword){
        if (dbPassword.startsWith("{md5}")){
            //如果是MD5加密
            String mdPassword = DigestUtils.md5DigestAsHex(password.getBytes());
            return  mdPassword.equals(dbPassword.replace("{md5}",""));
        }else if (dbPassword.startsWith("{bcrypt}")){
            //如果是bcrypt加密
            return BCrypt.checkpw(password,dbPassword.replace("{bcrypt}",""));
        }
        // 未加密
        return password.equals(dbPassword);
    }

    public static void main(String args[]){
        String password = "1234";//$2a$10$ofPkBDUezOJp6Sik63Q/0.QlU8a1itEyzldjSXqfn2nDPqXjN0Ljm
        String pwt = BCrypt.hashpw(password, BCrypt.gensalt()); // 加密
        System.out.println(pwt);
        boolean checkpw = BCrypt.checkpw(password, pwt);  // 解密
        System.out.println(checkpw);

    }
}
